<template>
    <div class="shouye">
        <div class="box_top">
            <div class="top">
                <h1>饿了吗</h1>
                <h2>···</h2>
            </div>
            <div class="input">
                <input type="text" placeholder="请输入搜索内容" />
            </div>
        </div>
        <div class="box_banner">
            <div class="banner"></div>
        </div>
        <div class="box_center">
            <h1>猜你喜欢</h1>
        </div>
        <div class="main">
            <div class="nav">
                <p v-for="(item, index) in arr" :key="index" @click="paixu(item, index)"
                    :class="indexs == index ? 'change' : ''">
                    {{ item }}
                </p>
            </div>
            <div class="list">
                <div class="shop" v-for="(item, index) in newlist ? newlist : list" :key="index">
                    <img :src="item.picUrl" alt="" />
                    <div class="text">
                        <h5>{{ item.name }}</h5>
                        <h3>{{ item.monthSalesTip }}</h3>
                        <h4>{{ item.distance }}</h4>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>

import { shop_list } from '@/ajax/api'

export default {
  data(){
    return {
      arr:['综合排序' , '距离最近' , '销量最高' , '筛选'],
      list:null,
      indexs:null,
      newlist:null

    }
  },
  methods:{
    paixu(item , index){
      if(this.indexs == index){
        this.indexs = null
        this.newlist = null
      }else{
        this.indexs = index
        if(item == '距离最近'){
          this.newlist = [...this.list]
          for(let i = 0 ; i < this.newlist.length ; i++){
            for(let j = i + 1 ; j < this.newlist.length ; j++){
              if(parseInt(this.newlist[i].distance) > parseInt(this.newlist[j].distance)){
                var temp
                temp = this.newlist[i]
                this.newlist[i] = this.newlist[j]
                this.newlist[j] = temp
              } 
            }
          }
        }else if(item == '销量最高'){
          this.newlist = [...this.list]
          for(let i = 0 ; i < this.newlist.length ; i++){
            for(let j = i + 1 ; j < this.newlist.length ; j++){
              if(parseInt(this.newlist[i].monthSalesTip.slice(2)) < parseInt(this.newlist[j].monthSalesTip.slice(2))){
                var temp
                temp = this.newlist[i]
                this.newlist[i] = this.newlist[j]
                this.newlist[j] = temp
              } 
            }
          }
        }else{
          this.newlist = null
        }
      }
    }
  },
  mounted(){
    shop_list().then((res) => {
      this.list = res.data.list
      console.log(this.list);
    })
  }
}
</script>

<style scoped>
.box_top {
    height: 80px;
    box-sizing: border-box;
    padding: 10px;
    background-color: cornflowerblue;
}

.top {
    width: 100%;
    display: flex;
    justify-content: space-between;
    height: 30px;
    color: #fff;
}

.input {
    width: 100%;
    height: 30px;
}

input {
    height: 30px;
    border: 1px solid #eee;
    border-radius: 30px;
    width: 90%;
    margin-left: 2%;
    padding-left: 20px;
}

.box_banner {
    width: 100%;
    box-sizing: border-box;
    padding: 10px;
    height: 150px;
}

.banner {
    width: 100%;
    background-color: blueviolet;
    border-radius: 5px;
    height: 100%;
}

.box_center {
    height: 30px;
    padding-left: 10px;
}

.box_center h1 {
    line-height: 30px;
    font-size: 20px;
    font-weight: 700;
}

.nav {
    padding: 0 10px;
    height: 30px;
    display: flex;
    justify-content: space-around;
}

p {
    height: 20px;
    width: 21%;
    background-color: #eee;
    border-radius: 5px;
    margin-top: 5px;
    text-align: center;
}

.list {
    margin-top: 20px;
    height: 310px;
    overflow-y: auto;
}

.shop {
    margin-bottom: 10px;
    height: 120px;
    display: flex;
    box-sizing: border-box;
    padding: 10px;
}

.shop img {
    height: 100px;
    margin-top: 10px;
    width: 100px;
}

.shop .text {
    padding: 20px;
    box-sizing: border-box;
}

h5,
h3,
h4 {
    height: 30px;
    font-size: 12px;
}

.change {
    color: rgb(235, 77, 77);
}
</style>